﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.ApproveCommand;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.ApproveCommandHandler
{
    public class DelLeaveModelCommandHandler : IRequestHandler<DelLeaveModelCommand, APIResult<object>>
    {
        private readonly IBaseRepository<LeaveModel> leaveRepo;
        private readonly IMapper mapper;

        public DelLeaveModelCommandHandler(IBaseRepository<LeaveModel> leaveRepo, IMapper mapper)
        {
            this.leaveRepo = leaveRepo;
            this.mapper = mapper;
        }
        public async Task<APIResult<object>> Handle(DelLeaveModelCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var obj = leaveRepo.GetAll().Result.FirstOrDefault(x => x.LeaveId == request.LeaveId);
            obj.IsDeleted = true;
            var res = leaveRepo.Update(obj).Result;
            if (res > 0)
            {
                result.Code = APIEnum.删除成功;
                result.Msg = APIEnum.删除成功.ToString();
                result.data = res;
            }
            else
            {
                result.Code = APIEnum.删除失败;
                result.Msg = APIEnum.删除失败.ToString();
                result.data = res;
            }

            return await Task.FromResult(result);


        }
    }
}
